# -*- coding: utf-8 -*-
import sys, traceback
import logging
from lxml import etree
from xadmin.model import *
from xadmin.lib.base import *
from xadmin.lib.associations import Association

class CRPRPRODAssociation(Association):
    def get(self):
        c.crprprodassociations = Session.query(CrPrAssociation).all()
        return render('/associations/crprprod.mako')

    def getByProject(self, project):
        try:
            project = Session.query(Project).get(project)
            associations = Session.query(ClientRequirement).add_entity(CrPrAssociation).add_entity(PrProdAssociation).join(['product_requirement_assoc', 'product_requirements', 'products_assoc']).filter(ClientRequirement.project_id==project.id).all()
            c.associations = []
            for association in associations:
                c.associations.append((association[1].cr_id, association[1].pr_id, association[2].prod_id))
            return render('/associations/crprprod.mako')
        except:
            print "ERROR: ", sys.exc_info()[0]
            return ''

    def create(self, project, data):
        #try:
        project = Session.query(Project).get(project)
        tree = etree.fromstring(data.encode())
        associations = tree.xpath('/associations/association')
        for association in associations:
            cr_aux = association[0].xpath('id')
            if cr_aux is not None:
                cr_id = cr_aux[0].text
            pr_id = association[1].xpath('id')[0].text
            prod_aux = association[2].xpath('id')
            if prod_aux is not None:
                prod_id = prod_aux[0].text
            if cr_id is not None and pr_id is not None:
                res = self._exists(cr_id, pr_id)
                if res is None:
                    cr = Session.query(ClientRequirement).get(cr_id)
                    pr = Session.query(ProductRequirement).get(pr_id)
                    prod = Session.query(Product).get(prod_id)
                    assoc = CrPrAssociation()
                    assoc.product_requirements = pr
                    cr.product_requirement_assoc.append(assoc)
                    Session.save(assoc)
                    Session.commit()
                else:
                    assoc = res
            if pr_id is not None and cr_id is not None:
                res2 = self._exists2(pr_id, prod_id)
                if res2 is None:
                    assoc2 = PrProdAssociation()
                    assoc2.products = prod
                    pr.products_assoc.append(assoc2)
                    Session.save(assoc2)
                    Session.commit()
                else:
                    assoc = res2
        return assoc
        #except:
        #    print "ERROR: ", sys.exc_info()[0]
        #    traceback.print_exc(file=sys.stdout)
        #    return None

    def delete(self):
        pass

    def _exists(self, cr_id, pr_id):
        try:
            assoc = Session.query(CrPrAssociation).filter_by(cr_id=cr_id, pr_id=pr_id).one()
            print assoc
            return assoc
        except:
            return None

    def _exists2(self, pr_id, prod_id):
        try:
            assoc = Session.query(PrProdAssociation).filter_by(pr_id=pr_id, prod_id=prod_id).one()
            return assoc
        except:
            return None